import request from '@/common/request'
import { getSetter } from '@/utils'

export default {
  namespaced: true,
  state: {
    loading: false,
    tableData: [],
    detail: {
      id: 0,
      title: '',
      cover_url: '',
      level: '',
      remark: '',
    },
    content: [],
    contentDetail: {
      audio_list: [],
      content_list: [],
      course_id: 0,
      id: 0,
      read_type: 1,
      remark: '',
      turn_point: [],
    },
  },
  mutations: {
    setLoading: getSetter('loading'),
    setTableData: getSetter('tableData'),
    setDetail: getSetter('detail'),
    setContent: getSetter('content'),
    setContentDetail: getSetter('contentDetail'),
  },
  actions: {
    async getList({ commit }, params) {
      commit('setLoading', true)
      const res = await request({
        url: '/apikanke/courseware/list',
        method: 'get',
        params,
      })
      if (res.code === 200) {
        commit('setTableData', res.data.data || [])
      }
      commit('setLoading', false)
      return res
    },
    createBook(context, data) {
      return request({
        url: '/apikanke/courseware/add',
        method: 'post',
        data,
      })
    },
    updateBook({ commit }, data) {
      return request({
        url: '/apikanke/courseware/update',
        method: 'post',
        data,
      })
    },
    async getDetail({ commit }, params) {
      commit('setLoading', true)
      const res = await request({
        url: '/apikanke/courseware/detail',
        method: 'get',
        params,
      })

      if (res.code == 200) {
        commit('setDetail', res.data || {})
      }
      commit('setLoading', false)

      return res
    },
    async getContent({ commit }, params) {
      commit('setLoading', true)

      const res = await request({
        url: '/apikanke/courseware/getContent',
        method: 'get',
        params,
      })
      if (res.code == 200) {
        commit('setContent', res.data || [])
      }
      commit('setLoading', false)

      return res
    },
    addUnit({ commit }, data) {
      return request({
        url: '/apikanke/courseware/addUnit',
        method: 'post',
        data,
      })
    },
    updateUnit({ commit }, data) {
      return request({
        url: '/apikanke/courseware/updateUnit',
        method: 'post',
        data,
      })
    },
    addContent({ commit }, data) {
      return request({
        url: '/apikanke/courseware/addContent',
        method: 'post',
        data,
      })
    },
    updateContent({ commit }, data) {
      return request({
        url: '/apikanke/courseware/updateContent',
        method: 'post',
        data,
      })
    },

    async getContentDetail({ commit }, params) {
      commit('setLoading', true)
      const res = await request({
        url: '/apikanke/courseware/getContentDetail',
        method: 'get',
        params,
      })
      if (res.code == 200) {
        commit('setContentDetail', res.data || [])
      }
      commit('setLoading', false)

      return res
    },
    copyCourse({ commit }, data) {
      return request({
        url: '/apikanke/courseware/copy',
        method: 'post',
        data,
      })
    },
    publishCourse({ commit }, data) {
      return request({
        url: '/apikanke/courseware/publishCourse',
        method: 'post',
        data,
      })
    },

    deleteCourse({ commit }, data) {
      return request({
        url: '/apikanke/courseware/delete',
        method: 'post',
        data,
      })
    },
    unpublishCourse({ commit }, data) {
      return request({
        url: '/apikanke/courseware/unpublishCourse',
        method: 'post',
        data,
      })
    },
    deleteContent({ commit }, data) {
      return request({
        url: '/apikanke/courseware/deleteContent',
        method: 'post',
        data,
      })
    },

    deleteUnit({ commit }, data) {
      return request({
        url: '/apikanke/courseware/deleteUnit',
        method: 'post',
        data,
      })
    },
    deleteBookCourse({ commit }, data) {
      return request({
        url: '/apikanke/courseware/deleteBookCourse',
        method: 'post',
        data,
      })
    },
    addSelectUnit({ commit }, data) {
      return request({
        url: '/apikanke/courseware/addSelectUnit',
        method: 'post',
        data,
      })
    },
    handleSort(context, data) {
      return request({
        url: '/apikanke/courseware/handleSort',
        method: 'post',
        data,
      })
    },
  },
}
